package com.zhouchao.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zhouchao.bean.Info;

/**
 * 消息一览
 * 
 * @author zhouchao
 *
 */
public class InfoDao {

	public List<Info> infoList(Connection con)throws Exception {
		List<Info> infolist = new ArrayList<Info>();
		String sql = "select * from t_info order by id desc";
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while(rs.next()) {
			Info info=new Info();
			info.setId(rs.getInt("id"));
			info.setType(rs.getInt("type"));
			info.setTitle(rs.getString("title"));
			info.setContent(rs.getString("content"));
			info.setName(rs.getString("name"));
			info.setDate(rs.getString("date"));
			
			infolist.add(info);
		}
		return infolist;
	}
	public Info infoList(Connection con,int id)throws Exception {
		Info info=new Info();
		String sql = "select * from t_info where id="+id;
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while(rs.next()) {
			
			info.setId(rs.getInt("id"));
			info.setType(rs.getInt("type"));
			info.setTitle(rs.getString("title"));
			info.setContent(rs.getString("content"));
			info.setName(rs.getString("name"));
			info.setDate(rs.getString("date"));
			
			System.out.println(info);
		}
		return info;
	}
	//添加消息
	public int intoInfo(Connection con,Info info) throws Exception{
		String sql = "insert into t_info values(null,?,?,?,?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, info.getType());
		pstmt.setString(2, info.getTitle());
		pstmt.setString(3, info.getContent());
		pstmt.setString(4, info.getName());
		pstmt.setString(5, info.getDate());
		pstmt.setString(6, "null");
		System.out.println(sql);
		return pstmt.executeUpdate();
	}
	
	
}
